package com.bai.ks2022.controller;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class adminController {
    @GetMapping("/{type}/{path}")
    public String toDetail(@PathVariable("type") String type, @PathVariable("path") String path, Model model) {
        model.addAttribute("key1", "value1");
        return  type + "/" + path;
    }

    @GetMapping("/login")
    public String toLoginPage(){
        return "login";
    }

    @PostMapping("/login")
    public String toLoginPage1(){return "login";}

//    @GetMapping("/index")
//    public String index(){
//        return "index";
//    }

    //获取用户权限
    @GetMapping("/index")

    public String getUser(Model model){
        SecurityContext context = SecurityContextHolder.getContext();
        System.out.println("userDetails:"+context);
        model.addAttribute("context", context);
        Authentication authentication = context.getAuthentication();
        UserDetails principal = (UserDetails) authentication.getPrincipal();
        model.addAttribute("principal", principal);
        System.out.println(principal);
        System.out.println("username:"+principal.getUsername());
        return "index";
    }
}
